<?php
	require_once("includes/init.inc.php");
	require_once("includes/api.files.php");
	
	$successMessage = "";
	if(isset($_GET["success"])){
		$successMessage = 
						"<tr><td colspan='2' align='center'> 
							<div style='text-align:center; color:green;'>The file has been successfully sent.</div>
						</td></tr>";
	}
	
	// cherche le ID de l'usager
	$user_id = $_SESSION['user_id'];
	
	mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWD) or die(mysql_error());
	mysql_select_db(DB_NAME) or die(mysql_error());
	
	// on crée le script JS qui va ajouter les tags au array
	$tagsScript = "<script>";
	// array qui contient les tags à afficher
	$tags_array = array();
	$result = mysql_query("SELECT id, name FROM label WHERE user_id = {$user_id}");
	while($row = mysql_fetch_array( $result )){
		// ajouter l'appel à la function JS pour ajouter le tag au Array() JS
		$tagsScript .= "addTag({$row[0]});";
		array_push(	$tags_array, array("id" => $row[0], "name" => $row[1]));
	}
	$tagsScript .= "</script>";
	
	// on crée le script JS qui va ajouter les fichiers au array
	$temp = array();
	$files_array = array();
	//$result = mysql_query("SELECT id, name FROM file WHERE user_id = {$user_id}");
	$result = mysql_query(
		"SELECT file.id, file.name, b.label_id FROM file_label b 
		LEFT OUTER JOIN label ON label.id = b.label_id
		LEFT OUTER JOIN file ON b.file_id = file.id
		WHERE label.user_id = {$user_id}"
	);
	$lastID = null;
	while($row = mysql_fetch_array( $result )){
		if(!isset($temp[$row[0]]))
			$temp[$row[0]] = array();
		array_push($temp[$row[0]], $row[2]);
		// ajouter le résultat de cette ligne dans le array $files_array
		// seulement s'il n'a pas déjà été créé
		if($lastID == null OR $lastID != $row[0])
			array_push(	$files_array, array("id" => $row[0], "name" => $row[1]));
		$lastID = $row[0];
	}
	$filesScript = "<script>";
	$filesScript .= "files = ".json_encode($temp).";";
	$filesScript .= "</script>";
	
	include("includes/header.inc.php");
	
	$counter = 0;
?>
				<?php echo $tagsScript ?>
				<?php echo $filesScript ?>
				
				<td id="contenu" style="height:300px;">
					<!-- Contenu commence ici -->
					<table border="0" cellpadding="0" cellspacing="0" width="100%">
						<?php echo $successMessage;?>
						<tr>
							<td width="30%">Tags</td>
							<td width="70%">Items in category</td>
						</tr>
						<tr>
							<td>
								<?php foreach ($tags_array as $counter => $array): ?>
									<div>
										<input type='checkbox' id='tag<?php echo $array["id"] ?>'
												onclick="taggClicked(<?php echo $array["id"] ?>)"
												<?php if($array["name"]=="All") echo 'checked'; ?>/><label for='tag<?php echo $array["id"] ?>'><?php echo $array["name"] ?></label>
									</div>
								<?php endforeach; ?>
							</td>
							<td>
								<?php foreach ($files_array as $counter => $array): 
									if($counter%4==0 AND $counter!=0)
										echo "<div>";
									?>
									<div class="displayFile" id="file<?php echo $array["id"] ?>">
										<div align="center" onclick="clickedFileAction(<?php echo $array["id"] ?>)">
										<?php
											$info = get_file_info($array["id"]);
											echo '<img src="'.$info['icon'].'" alt="'.$info['name'].'"/>';
										?>
										</div>
										<div class="smallFileName" onclick="clickedFileAction(<?php echo $array["id"] ?>)"> <?php echo $array["name"] ?> </div>
									</div>
									
								<?php
									if($counter%4==0 AND $counter!=0)
										echo "<div style='clear:both;'/></div>";
									$counter ++; 
									endforeach; ?>
							</td>
						</tr>
					</table>
					<!-- Contenu fini ici -->
				</td>
<?php
	include("includes/footer.inc.php");
?>